package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.Area;
import com.iot.manage.pojo.Device;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface AreaMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.AREA_ID areaId,\n" +
            " temp.AREA_NAME areaName\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_AREA t\n" +
            "\tWHERE t.AREA_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.AREA_ID\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<Area> findAreaList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.AREA_ID) FROM T_AREA a WHERE a.AREA_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countAreaList(String query);

    @Insert("<script>INSERT INTO T_AREA (AREA_ID,AREA_NAME) VALUES (#{areaId},#{areaName})</script>")
    void addArea(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.AREA_ID areaId,  \n" +
            " temp.AREA_NAME areaName\n" +
            "FROM T_AREA temp\n" +
            "\tWHERE temp.AREA_ID = #{params} OR temp.AREA_NAME = #{params}\n" +
            "</script>")
    Area findAreaByIdOrName(String params);

    @Update("<script>" +
            "UPDATE T_AREA SET \n" +
            "\tAREA_ID = #{areaId},\n" +
            "\tAREA_NAME = #{areaName}\n" +
            "WHERE AREA_ID = #{areaId}" +
            "</script>")
    void updateAreaInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_AREA WHERE AREA_ID = #{areaId}</script>")
    void deleteAreaById(String areaId);

    @Select("<script>SELECT a.DEVICE_ID deviceId,a.DEVICE_NAME deviceName FROM T_DEVICE a</script>")
    List<Device> findAllAllotDevice();

    @Select("<script>SELECT a.DEVICE_ID deviceId FROM T_DEVICE a WHERE a.DEVICE_ID IN (SELECT DEVICE_ID FROM T_AREA_DEVICE WHERE AREA_ID = #{areaId})</script>")
    List<String> findAllotDeviceById(String areaId);

    @Delete("<script>DELETE FROM T_AREA_DEVICE WHERE AREA_ID = #{areaId}</script>")
    void deleteDeviceOfAllotById(String areaId);

    @Insert("<script>" +
            "INSERT INTO T_AREA_DEVICE (AREA_ID,DEVICE_ID)\n" +
            "<foreach collection =\"list\" item=\"areaDevice\" separator =\"union all\">\n" +
            "\t\t (select\n#{areaDevice.areaId},#{areaDevice.deviceId} from dual )\n" +
            "</foreach >" +
            "</script>")
    void addDeviceOfAllotById(List<Map<String, String>> list);

    @Delete("<script>DELETE FROM T_APP_AREA WHERE AREA_ID = #{areaId}</script>")
    void deleteRelevancyAppById(String areaId);

    @Delete("<script>DELETE FROM T_AREA_DEVICE WHERE AREA_ID = #{areaId}</script>")
    void deleteRelevancyDeviceById(String areaId);
}
